<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="服务类型" v-bind="validateInfos.signingServiceId">
	          <j-select-multiple type="list_multi" v-model:value="formData.signingServiceId" dictCode="caishui_signing_service,name,id" :disabled="disabled" :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="编码" v-bind="validateInfos.code">
            <a-input v-model:value="formData.code" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="姓名" v-bind="validateInfos.name">
            <a-input v-model:value="formData.name" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="手机号码" v-bind="validateInfos.phone">
            <a-input v-model:value="formData.phone" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
<!--        <a-col :span="24">
          <a-form-item label="性别" v-bind="validateInfos.sex">
            <j-dict-select-tag type='radio' v-model:value="formData.sex" dictCode="sex" placeholder="请选择性别" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="生日" v-bind="validateInfos.birthday">
		        <a-date-picker placeholder="请选择生日" v-model:value="formData.birthday" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="民族" v-bind="validateInfos.nation">
            <a-input v-model:value="formData.nation" placeholder="请输入民族" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>-->
        <a-col :span="24">
          <a-form-item label="证件类型" v-bind="validateInfos.idCardType">
	          <j-dict-select-tag v-model:value="formData.idCardType" stringToNumber="true" dictCode="id_card_type" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="证件号码" v-bind="validateInfos.idCard">
            <a-input v-model:value="formData.idCard" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12" >
          <a-form-item label="证件正面照" v-bind="validateInfos.idCardZm" :label-col="{ span: 10 }" :wrapper-col="{ span: 14 }">
	          <j-image-upload  v-model:value="formData.idCardZm" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="12" >
          <a-form-item label="证件反面照" v-bind="validateInfos.idCardFm" :label-col="{ span: 10 }" :wrapper-col="{ span: 14 }">
	          <j-image-upload  v-model:value="formData.idCardFm" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
<!--        <a-col :span="24">
          <a-form-item label="证件地址" v-bind="validateInfos.idCardAddress">
	          <a-textarea v-model:value="formData.idCardAddress" rows="4" placeholder="请输入证件地址" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="发证机关" v-bind="validateInfos.idCardOffice">
            <a-input v-model:value="formData.idCardOffice" placeholder="请输入发证机关" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="证件有效期止" v-bind="validateInfos.idCardValid">
            <a-input v-model:value="formData.idCardValid" placeholder="请输入证件有效期止" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>-->
        <a-col :span="24">
          <a-form-item label="收款类型" v-bind="validateInfos.bankCardType">
	          <j-dict-select-tag v-model:value="formData.bankCardType" stringToNumber="true" dictCode="bank_card_type" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="收款账号" v-bind="validateInfos.bankCardAccount">
            <a-input v-model:value="formData.bankCardAccount" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
<!--        <a-col :span="24">
          <a-form-item label="支付宝ID" v-bind="validateInfos.bankZfbId">
            <a-input v-model:value="formData.bankZfbId" placeholder="请输入支付宝ID" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微信ID" v-bind="validateInfos.bankWxId">
            <a-input v-model:value="formData.bankWxId" placeholder="请输入微信ID" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>-->
        <a-col :span="12">
          <a-form-item label="签约类型" v-bind="validateInfos.signingType" :label-col="{ span: 10 }" :wrapper-col="{ span: 8 }">
	          <j-dict-select-tag v-model:value="formData.signingType" stringToNumber="true" dictCode="caishui_member_signing_type" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="签约状态" v-bind="validateInfos.signingStatus" :label-col="{ span: 10 }" :wrapper-col="{ span: 8 }">
	          <j-dict-select-tag v-model:value="formData.signingStatus" stringToNumber="true" dictCode="caishui_member_signing_status" :disabled="disabled"/>
          </a-form-item>
        </a-col>
<!--        <a-col :span="24">
          <a-form-item label="签约备注/拒绝原因" v-bind="validateInfos.signingRemark">
            <a-input v-model:value="formData.signingRemark" placeholder="请输入签约备注/拒绝原因" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>-->
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../SigningMember.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    companyId: '',   
    businessId: '',   
    signingServiceId: '',   
    code: '',   
    name: '',   
    phone: '',   
    sex: undefined,
    birthday: '',   
    nation: '',   
    idCardType: undefined,
    idCard: '',   
    idCardZm: '',   
    idCardFm: '',   
    idCardAddress: '',   
    idCardOffice: '',   
    idCardValid: '',   
    bankCardType: '',   
    bankCardAccount: '',   
    bankZfbId: '',   
    bankWxId: '',   
    signingType: undefined,
    signingFile: '',   
    status: undefined,
    signingStatus: undefined,
    signingRemark: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    companyId: [{ required: true, message: '请输入税源地ID!'},],
    businessId: [{ required: true, message: '请输入商户ID!'},],
    signingServiceId: [{ required: true, message: '请输入签约服务类型ID（多个英文逗号隔开）!'},],
    name: [{ required: true, message: '请输入姓名!'},],
    phone: [{ required: true, message: '请输入手机号码!'},],
    idCardType: [{ required: true, message: '请输入证件类型：1-身份证，2-港澳台通行证，3-护照，4-军官证!'},],
    idCard: [{ required: true, message: '请输入身份证号码!'},],
    signingType: [{ required: true, message: '请输入签约类型：1-e签宝，2-批量导入!'},],
    status: [{ required: true, message: '请输入用户状态：1-正常，2-冻结!'},],
    signingStatus: [{ required: true, message: '请输入签约状态：0-待签约，1-签约中，2-已签约，3-失败，4-拒签!'},],
  };
  const { resetFields, validate, validateInfos, clearValidate } = useForm(formData, validatorRules, { immediate: true });

   // 表单禁用
   const disabled = computed(()=>{
      let bool = false;
      if(props.formBpm === true){
        if(props.formData.disabled === false){
          bool = false;
        }else{
          bool =  true;
        }
      }
      bool = props.formDisabled;
      if(bool){
        //清除表单验证
        clearValidate();
      }
      return bool;
   });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //数据格式化
    let model = formData;
    if (model.id) {//编辑
       isUpdate.value = true;
       model.updateBy = "";
       model.updateTime = "";
    }else {//新增
       model.createBy = "";
       model.createTime = "";
       model.sysOrgCode = "";
       model.updateBy = "";
       model.updateTime = "";
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
